
function MFAgetUserSettings () {
   var str = "&showfed="+localStorage.getItem("po_mfa_show_fed");
   str += "&showuni="+localStorage.getItem("po_mfa_show_uni");
   str += "&showemp="+localStorage.getItem("po_mfa_show_emp");
   str += "&showneu="+localStorage.getItem("po_mfa_show_neu");
   return str;
}

function MFArefreshData (force, cb) {
    // Get the date of the last version check. If it's more than one day old, check for new versions. Otherwise exit.
    var lastCheck = localStorage.getItem ("po_mfa_lastCheck");
    if (!lastCheck)
        lastCheck = 0;
    else
        lastCheck = Number (lastCheck);

    var today = new Date ();
    var todayCheck = Number(today.getTime());

    // Check for fresh data only each five minutes or if there's no stored info
    if (!force && todayCheck < (lastCheck+300000) && localStorage.getItem ('po_mfa_alerts')) {
      return;
    }

    var url = POgetServerUrl ("alerter/po_mfa_feed.php", false);
    //var url = POgetServerUrl ("jirinatest/getalerts-1.0.php", false);
    var params = POgetServerParams ();
    params += MFAgetUserSettings ();
    var callback = function (httpRequest) {
//POlog ("po_mfa_menu: ReadyState:"+httpRequest.readyState+" Response:"+httpRequest.statusText);// + " Status:"+httpRequest.status);
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var response = httpRequest.responseText;
            localStorage.setItem ('po_mfa_alerts', response);
            localStorage.setItem ("po_mfa_lastCheck", String((new Date().getTime())));
//POlog ("po_mfa_menu: Data arrived:"+response);
            setTimeout(MFArefreshData,300000);
            if (cb) cb(true);
         } else if (httpRequest.readyState == 4 && httpRequest.status != 200) {
//POlog ("po_mfa_menu: Connection error. Response:"+httpRequest.statusText);
            localStorage.setItem ("po_mfa_lastCheck", String((new Date().getTime())));
            setTimeout(MFArefreshData,300000);
            if (cb) cb(false);
         }
    }
    
    // Remove the following line if we go to production
    params += "&time="+(new Date()).getTime ();
//POlog ("po_mfa_menu: Sending request: URL:"+url+" Params:"+params);    
    POajaxPost (url, "GET", params, callback);

//POlog ("po_mfa_menu: Data request sent. Url:"+url+" Params:"+params);
}
top.po_mfa_refresh = MFArefreshData;

// To ensure that the po_utils.js is already loaded
MFArefreshData();
